package com.weipai.springboot.module.service.impl;

import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Apk;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.mapper.ApkMapper;
import com.weipai.springboot.module.service.ApkService;
import com.weipai.springboot.module.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @Author: lyh
 * @Date: 2019/3/1 11:58
 * @Version 1.0
 */
@Service
public class ApkServiceImpl implements ApkService {

    @Autowired
    private ApkMapper apkMapper;


    @Override
    public Response save(Apk apk) {
        Response response = new Response();
        apk.setId(UUID.randomUUID().toString());
        apk.setCtsstr(DateUtils.timeFormat(System.currentTimeMillis()));
        apk.setCts(System.currentTimeMillis());
        apk.setUpdateTs(System.currentTimeMillis());
        apk.setSort(0);
        apk.setFlag(0);
        if(apkMapper.insertSelective(apk)>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    @Override
    public Response getNewapkVersions() {
        Response response = new Response();
        List<Apk> apkList =  apkMapper.selectAll();
        if(apkList.size()>0){
            Apk apk = apkList.get(0);
            response.setCode(RetCode.SUCCESS);
            response.setData(apk);
        }else {
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }
}
